import {transformI18n} from "@/plugins/i18n";
import {getDictValueList} from "@/api/system";
import {getConversionStats,} from "@/api/ads";
import {onMounted, reactive, ref, type Ref, toRaw} from "vue";
import {useRoute} from 'vue-router';

export function useShop(treeRef: Ref) {
  const route = useRoute();
  const form = reactive({
    shop_name: "",
    status: "",group: +route.query?.group||"",
    standard_code: ""
  });
  const curRow = ref();
  const formRef = ref();
  const dataList = ref([]);
  const groupList = ref([]);
  const isShow = ref(false);
  const loading = ref(true);
  const isLinkage = ref(false);

  const columns: TableColumnList = [
    {label: "店铺id", prop: "store_id"},
    {label: "分组", prop: "group", minWidth: 80,cellRenderer: scope => (groupList.value.find(x=>scope.row.group==x.id)?.group_name||'-')},
    {label: "店铺名称", prop: "shop_name", minWidth: 120},
    {label: "店铺域名", prop: "shop_domain", minWidth: 150},
    {label: "服务开始时间", prop: "starts_at",width:130},
    {label: "服务截止时间", prop: "ends_at",width:130},
    {label: "套餐版本ID", prop: "package_type"},
    {label: "时区", prop: "time_zone"},

    {label: "备注", prop: "remark"},
    {label: "币种", prop: "standard_code",width:70},
    {label: "汇率", prop: "exchange_rate",width:70},
    {label: "创建时间", prop: "created_at", width: 160},
    {label: "操作", fixed: "right", width: 140, slot: "operation"}
  ];




  async function onSearch() {
    loading.value = true;
    const { data } = await getConversionStats(toRaw(form),'get');
    dataList.value = data.data;
    groupList.value = data.group;


    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };


  const dictList = ref([])
  onMounted(async () => {
    onSearch();
    const { data } = await getDictValueList(toRaw({limit:10000}));
    dictList.value = data.data;
  });



  return {
    form,
    isShow,
    curRow,
    loading,
    columns,dictList,groupList,
    dataList,
    isLinkage,
    onSearch,
    resetForm,
    transformI18n,
  };
}
